<template>
	<view>
		<view class="input-item">
			<view class="title"><text class="iconfont" style="color: #e64340;">&#xe611;</text>教师编号</view>
			<view class="input-content">
				<input class="text-input" type="text" disabled v-model="tid" />
			</view>
		</view>
		<view class="input-item">
			<view class="title"><text class="iconfont" style="color: #4edfe6;">&#xebe4;</text>操作类型</view>
			<view class="input-content">
				<picker @change="optionTypeIndex=$event.detail.value" :value="optionTypeIndex" :range="optionType">
					<text>{{optionType[optionTypeIndex]}}</text>
				</picker>
			</view>
		</view>
		<view class="input-item" v-if="achievementApplyType=='1'&&optionTypeIndex!=0">
			<view class="title"><text class="iconfont" style="color: #2f32e6;">&#xe7cb;</text>专著编号</view>
			<view class="input-content">
				<input class="text-input" type="text" v-model="treatiseId" />
			</view>
		</view>
		<view class="input-item" v-if="achievementApplyType=='2'&&optionTypeIndex!=0">
			<view class="title"><text class="iconfont" style="color: #e6e61a;">&#xe7cb;</text>论文编号</view>
			<view class="input-content">
				<input class="text-input" type="text" v-model="paperId" />
			</view>
		</view>
		<view class="input-item" v-if="achievementApplyType=='3'&&optionTypeIndex!=0">
			<view class="title"><text class="iconfont" style="color: #e6ad86;">&#xe7cb;</text>专利编号</view>
			<view class="input-content">
				<input class="text-input" type="text" v-model="patentId" />
			</view>
		</view>
		<view class="input-item" v-if="achievementApplyType=='4'&&optionTypeIndex!=0">
			<view class="title"><text class="iconfont" style="color: #801ae6;">&#xe7cb;</text>项目编号</view>
			<view class="input-content">
				<input class="text-input" type="text" v-model="projectId" />
			</view>
		</view>
		<view class="input-item" v-if="achievementApplyType=='5'&&optionTypeIndex!=0">
			<view class="title"><text class="iconfont" style="color: #86e609;">&#xe7cb;</text>奖励编号</view>
			<view class="input-content">
				<input class="text-input" type="text" v-model="awardId" />
			</view>
		</view>

		<view class="textarea">
			<textarea placeholder="请输入变更理由" v-model="con" />
		</view>
		<view class="bottom">
			<button type="primary" @click="submit"><text class="iconfont" style="color: #FFFFFF;">&#xe64a;</text>提交</button>
		</view>
	</view>
</template>
<script>
	
	import {
		treatiseApply, 
		paperApply, 
		patentApply,
		projectApply, 
		rewardApply,
		treatiseApplyEdit,
		paperApplyEdit,
		patentApplyEdit,
		projectApplyEdit,
		rewardApplyEdit,
		treatiseApplyDel,
		paperApplyDel,
		patentApplyDel,
		projectApplyDel,
		rewardApplyDel,
	 } from '@/api/tAchievementApply.js'
	
	function successHandel(res){
		// console.log(res)
		
		if(res.data.code==0){
			uni.showModal({
				content:'提交成功',
				showCancel:false,
				success: () => {
					uni.navigateBack()
				}
			})
		}else{
			uni.showModal({
				content:res.data.message,
				showCancel:false
			})
		}
	}
	
	function timeFormat() {
		const date = new Date()
		let year = date.getFullYear()
		let month = date.getMonth() + 1
		let day = date.getDate()
		let hour = date.getHours()
		let min = date.getMinutes()
		let second = date.getSeconds()
		month = month > 9 ? month : '0' + month
		day = day > 9 ? day : '0' + day;
		hour = hour > 9 ? hour : '0' + hour;
		min = min > 9 ? min : '0' + min;
		second = second > 9 ? second : '0' + second;
		return `${year}-${month}-${day} ${hour}:${min}:${second}`
	}
	
	export default {
		data() {
			return {
				tid:getApp().globalData.tid,
				achievementApplyType:'1',
				treatiseId:'',
				paperId:'',
				patentId:'',
				projectId:'',
				awardId:'',
				optionType:['新增','修改','删除'],
				optionTypeIndex:0,
				con: ''
			}
		},
		methods: {
			submit(){
				let data = {
					tid:this.tid,
					con:this.con,
					type:this.optionTypeIndex,
					time:timeFormat()
				}
				// console.log(data)
				// return 
				switch(this.achievementApplyType){
					case '1':{ //专著
						if(this.treatiseId){
							data.teaTreatiseId = this.treatiseId
						}
						if(data.type==0){
							treatiseApply(data).then(res=>{
								successHandel(res)
							}).catch(err=>{
								uni.showModal({
									content:'提交失败'
								})
							})
						}else if(data.type == 1){
							treatiseApplyEdit(data).then(res=>{
								successHandel(res)
							}).catch(err=>{
								uni.showModal({
									content:'提交失败'
								})
							})
						}else{
							treatiseApplyDel(data).then(res=>{
								successHandel(res)
							}).catch(err=>{
								uni.showModal({
									content:'提交失败'
								})
							})
						}
						break
					}// case1
					case '2':{ //论文
						if(this.paperId){
							data.teaPaperId = this.paperId
						}
						if(data.type==0){
							paperApply(data).then(res=>{
								successHandel(res)
							}).catch(err=>{
								uni.showModal({
									content:'提交失败'
								})
							})
						}else if(data.type==1){
							paperApplyEdit(data).then(res=>{
								successHandel(res)
							}).catch(err=>{
								uni.showModal({
									content:'提交失败'
								})
							})
						}else{
							paperApplyDel(data).then(res=>{
								successHandel(res)
							}).catch(err=>{
								uni.showModal({
									content:'提交失败'
								})
							})
						}
						break
					}//case2
					case '3':{ //专利
						if(this.patentId){
							data.teaPatentId = this.patentId
						}
						if(data.type==0){
							patentApply(data).then(res=>{
								successHandel(res)
							}).catch(err=>{
								uni.showModal({
									content:'提交失败'
								})
							})
						}else if(data.type==1){
							patentApplyEdit(data).then(res=>{
								successHandel(res)
							}).catch(err=>{
								uni.showModal({
									content:'提交失败'
								})
							})
						}else{
							patentApplyDel(data).then(res=>{
								successHandel(res)
							}).catch(err=>{
								uni.showModal({
									content:'提交失败'
								})
							})
						}
						break
					}//case3
					case '4':{ //项目
						if(this.projectId){
							data.teaProjectID=this.projectId
						}
						if(data.type==0){
							projectApply(data).then(res=>{
								successHandel(res)
							}).catch(err=>{
								uni.showModal({
									content:'提交失败'
								})
							})
						}else if(data.type==1){
							projectApplyEdit(data).then(res=>{
								successHandel(res)
							}).catch(err=>{
								uni.showModal({
									content:'提交失败'
								})
							})
						}else{
							projectApplyDel(data).then(res=>{
								successHandel(res)
							}).catch(err=>{
								uni.showModal({
									content:'提交失败'
								})
							})
						}
						break
					}//case4
					case '5':{ //奖励
						if(this.awardId){
							data.teaAwardId = this.awardId
						}
						if(data.type==0){
							rewardApply(data).then(res=>{
								successHandel(res)
							}).catch(err=>{
								uni.showModal({
									content:'提交失败'
								})
							})
						}else if(data.type==1){
						
							rewardApplyEdit(data).then(res=>{
								successHandel(res)
							}).catch(err=>{
								uni.showModal({
									content:'提交失败'
								})
							})
						}else{
							rewardApplyDel(data).then(res=>{
								successHandel(res)
							}).catch(err=>{
								uni.showModal({
									content:'提交失败'
								})
							})
						}
						break
					}//case5
					default:{
						uni.showModal({
							content:'提交失败'
						})
					}
				}//switch
			}//submit
		},
		onLoad(option) {
			/**
			 * 1：专著
			 * 2：论文
			 * 3：专利
			 * 4：项目
			 * 5：奖励
			 */
			this.achievementApplyType = option.achievementApplyType
		}
	}
</script>

<style>
	.input-item {
		height: auto;
		position: relative;
		height: 40px;
		margin: 0 10px;
		border-bottom: 1px solid #C0C0C0;
		font-family: 'Courier New', Courier, monospace;
	}
	.iconfont {
		font-family: "iconfont" !important;
		font-style: normal;
		-webkit-font-smoothing: antialiased;
		-moz-osx-font-smoothing: grayscale;
		color: #C0C0C0;
		font-size: 16px;
		margin-right: 5px;
	}
	.input-item .title {
		position: absolute;
		top: 0;
		left: 0;
		width: 30%;
		height: 100%;
		text-align: center;
		line-height: 40px;
	}

	.input-item .input-content {
		position: absolute;
		top: 0;
		right: 0;
		width: 70%;
		height: 100%;
	}

	.input-item .input-content .text-input {
		position: absolute;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 100%;
	}
	picker{
		margin-top: 5px;
	}
	
	.textarea{
		margin-top: 15px;
		padding: 0 10px;
	}
	.textarea textarea{
		width: 95%;
		border-radius: 10px;
		border: 1px solid #808080;
		padding: 5px 10px;
		font-family: 'Courier New', Courier, monospace;
	}
	.bottom{
		margin-top: 30px;
		padding: 0 10px;
	}
</style>
